<!-- 权限分配 -->
<template>
  <el-dialog :visible="visible" title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)" @close="dialogClose" @open="fetchPermisssionList">
    <!-- 权限点树形数据 -->
    <!-- data 树形组件的数据
    show-checkbox 树形组件的多选框
    node-key 开启多选框的时需要添加的唯一标识
    props 配置渲染的字段
    default-expand-all 是否默认展开所有节点
    check-strictly 取消父子关联 -->
    <el-tree
      ref="treeRef"
      :data="list"
      show-checkbox
      node-key="id"
      :props="defaultProps"
      default-expand-all
      check-strictly
    />
    <!-- 按钮区域 -->
    <template #footer>
      <div style="text-align: right;">
        <el-button @click="$emit('dialog-close')">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { getPermissionList, assignPermission, getRoleDetail } from '@/api'
import findSon from '@/utils/findSon'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    roleId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      list: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  methods: {
    // 弹窗关闭，执行的回调
    dialogClose() {
      this.$emit('dialog-close')
    },
    // 打开弹窗，执行的回调
    async fetchPermisssionList() {
      const { data } = await getPermissionList()
      this.list = findSon(data, '0')
      const { data: { permIds }} = await getRoleDetail(this.roleId)
      this.$refs.treeRef.setCheckedKeys(permIds)
    },
    // 权限提交
    async submit() {
      await assignPermission({
        id: this.roleId,
        permIds: this.$refs.treeRef.getCheckedKeys()
      })
      this.$message.success('分配权限成功')
      this.dialogClose()
    }

  }
}

</script>
<style lang='less' scoped>
</style>
